<?php
include_once('user.php');
class Connection {

    private $host = "localhost";
    private $userDB = "root";
    private $pass = "";
    private $bdd    = "emrs";
    private $table ;

    private static  $con = null;
    private $users = array();

    private function __construct() {

        $connect = mysql_connect("localhost","root","")
                    or die("Connection impossible");
        $db = mysql_select_db('emrs');
       /*     $user = new User();
            $user->setName("matthieu");
            $user->setLogin("froggy");
            $user->setPasswd("froggy");
            $this->users[0]=$user;

            $user = new User();
            $user->setName("eduard");
            $user->setLogin("redward");
            $user->setPasswd("redward");
            $this->users[1]=$user;

            $user = new User();
            $user->setName("renaud");
            $user->setLogin("ipefix");
            $user->setPasswd("ipefix");
            $this->users[2]=$user;

            $user = new User();
            $user->setName("salim");
            $user->setLogin("sal");
            $user->setPasswd("sal");
            $this->users[3]=$user;
           */

    }

    public static function getInstance(){
        if (self::$con === null){
            self::$con = new Connection();     
        }
        
        return self::$con;
    }

    public function createUser($user){
        $sql_request = "INSERT INTO `user` (`id`, `name`, `pswd`, `email`, `adr`, `birth`) VALUES (NULL, '".$user->getName()."', '".$user->getPasswd()."', '".$user->getLogin()."', '".$user->getAdress()."', '".$user->getDN()."');";
        $request = mysql_query($sql_request);
        if(!$request)
            throw new Exception('Probleme est survenu');
    }
    public function checkUserLogin($user){
        $sql_request="SELECT * FROM `user` WHERE email='".$user->getLogin()."' and pswd='".$user->getPasswd()."'";
        $result=mysql_query($sql_request);
        $count=mysql_num_rows($result);
        if($count>0){
            $row = mysql_fetch_array($result);
            $user->setName($row[1]);
            return $user;
           // print ("Hello ");

        }
        else { 
            throw new Exception('Un erreur est survenue : '. mysql_error());
        }   
  
    }
    public function getUsers(){return $this->users;}

    
}

?>
